/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

/**
 *
 * @author MrKoi
 */
public class ExitsBean {
    Connect c=new Connect();
    Connection con;
    public ExitsBean() {
    }
    //check exits customer in car booking
    public boolean ExitsCusInCarBook(String user)
    {
        con=c.getConnection();
        try
        {
            String sql="Select UserName from CarBookings where UserName=?";
            PreparedStatement psm=con.prepareStatement(sql);
            psm.setString(1,user);
            ResultSet rs=psm.executeQuery();
            while(rs.next())
            {
                if(rs.getString("UserName").equals(user))
                {
                    return true;
                }
            }
            psm.close();
            con.close();
        }catch (Exception ex){return false;}
        return false;
    }
    //check exist role admin
     public boolean ExitsAdmin(String user)
    {
        con=c.getConnection();
        try
        {
            String sql="Select [Role] from Admin where UserName=?";
            PreparedStatement psm=con.prepareStatement(sql);
            psm.setString(1,user);
            ResultSet rs=psm.executeQuery();
            while(rs.next())
            {
                if(rs.getInt("Role")==1)
                {
                    return true;
                }
            }
            psm.close();
            con.close();
        }catch (Exception ex){return false;}
        return false;
    }
    //check username exist in TourBooking
    public boolean ExitsCusInTourBook(String user)
    {
        con=c.getConnection();
        try
        {
            String sql="Select UserName from TourBookings where UserName=?";
            PreparedStatement psm=con.prepareStatement(sql);
            psm.setString(1,user);
            ResultSet rs=psm.executeQuery();
            while(rs.next())
            {
                if(rs.getString("UserName").equals(user))
                {
                    return true;
                }
            }
            psm.close();
            con.close();
        }catch (Exception ex){return false;}
        return false;
    }
    //check exist carid in carbook
    public boolean ExitsCarIDInCarBook(String user)
    {
        con=c.getConnection();
        try
        {
            String sql="Select CarID from CarBookings where CarID=?";
            PreparedStatement psm=con.prepareStatement(sql);
            psm.setString(1,user);
            ResultSet rs=psm.executeQuery();
            while(rs.next())
            {
                if(rs.getString("CarID").equals(user))
                {
                    return true;
                }
            }
            psm.close();
            con.close();
        }catch (Exception ex){return false;}
        return false;
    }
    
    //check exist carid in car
    public boolean ExitsCarIDInCar(int carid)
    {
        con=c.getConnection();
        try
        {
            String sql="Select ModelID from Cars where ModelID=?";
            PreparedStatement psm=con.prepareStatement(sql);
            psm.setInt(1,carid);
            ResultSet rs=psm.executeQuery();
            while(rs.next())
            {
                if(rs.getInt("ModelID")==carid)
                {
                    return true;
                }
            }
            psm.close();
            con.close();
        }catch (Exception ex){return false;}
        return false;
    }
    //check location id in hotel
    public boolean ExistlocationIDInHotel(int id)
    {
        con=c.getConnection();
        try
        {
            String sql="Select LocationID from Hotels where LocationID=?";
            PreparedStatement psm=con.prepareStatement(sql);
            psm.setInt(1,id);
            ResultSet rs=psm.executeQuery();
            while(rs.next())
            {
                if(rs.getInt("LocationID")==id)
                {
                    return true;
                }
            }
            psm.close();
            con.close();
        }catch (Exception ex){return false;}
        return false;
    }
    
     public boolean ExistlocationIDInFlight(int id)
    {
        con=c.getConnection();
        try
        {
            String sql="Select LocationID from Flights where LocationID=?";
            PreparedStatement psm=con.prepareStatement(sql);
            psm.setInt(1,id);
            ResultSet rs=psm.executeQuery();
            while(rs.next())
            {
                if(rs.getInt("LocationID")==id)
                {
                    return true;
                }
            }
            psm.close();
            con.close();
        }catch (Exception ex){return false;}
        return false;
    }
     public boolean ExistTourInTourBooking(String tourId){
        try {
            con=c.getConnection();
            CallableStatement cl=null;
            cl=con.prepareCall("{call ExistTourInTourBooking_sp(?)}");
            cl.setString("tourId", tourId);
            ResultSet rs= cl.executeQuery();
            if(rs.next())
                return true;
            else
                return false;
        } catch (SQLException ex) {
            Logger.getLogger(ExitsBean.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
     }
     public static  void main4(String[] args){
         ExitsBean eb= new ExitsBean();
         System.out.println(eb.ExistTourInTourBooking("T0002"));
     }
}
